#Boa:Frame:InventarioAjusteAprobar

import wx
import wx.grid
import consultasBD
import utiles
from decimal import Decimal

ROJO = (255, 145, 145, 255)
VERDE = (145, 255, 145, 255)
AZUL = (145, 145, 255, 255)
BLANCO = (255, 255, 255, 255)
AMARILLO = (254, 248, 191, 255)

def create(parent):
    return InventarioAjusteAprobar(parent)

[wxID_INVENTARIOAJUSTEAPROBAR, wxID_INVENTARIOAJUSTEAPROBARBUTTON1, 
 wxID_INVENTARIOAJUSTEAPROBARBUTTON2, wxID_INVENTARIOAJUSTEAPROBARBUTTON3, 
 wxID_INVENTARIOAJUSTEAPROBARBUTTON4, wxID_INVENTARIOAJUSTEAPROBARGRID1, 
 wxID_INVENTARIOAJUSTEAPROBARPANEL1, wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON1, 
 wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON2, 
 wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON3, 
 wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON4, 
 wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON5, 
 wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON6, 
 wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON7, 
 wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON8, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT1, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT10, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT11, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT12, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT2, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT3, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT4, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT5, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT6, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT7, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT8, 
 wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT9, 
 wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL1, wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL2, 
 wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL3, wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL4, 
 wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL5, wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL6, 
] = [wx.NewId() for _init_ctrls in range(33)]

class InventarioAjusteAprobar(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_INVENTARIOAJUSTEAPROBAR,
              name='InventarioAjusteAprobar', parent=prnt, pos=wx.Point(168,
              34), size=wx.Size(1132, 578), style=wx.DEFAULT_FRAME_STYLE,
              title='Ajuste de Inventario: aprobar')
        self.SetClientSize(wx.Size(1124, 551))

        self.panel1 = wx.Panel(id=wxID_INVENTARIOAJUSTEAPROBARPANEL1,
              name='panel1', parent=self, pos=wx.Point(0, 0), size=wx.Size(1124,
              551), style=wx.TAB_TRAVERSAL)

        self.radioButton1 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON1,
              label='Fecha', name='radioButton1', parent=self.panel1,
              pos=wx.Point(104, 8), size=wx.Size(64, 13), style=0)
        self.radioButton1.SetValue(True)

        self.radioButton2 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON2,
              label='Sucursal', name='radioButton2', parent=self.panel1,
              pos=wx.Point(104, 24), size=wx.Size(81, 13), style=0)
        self.radioButton2.SetValue(False)

        self.radioButton6 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON6,
              label='P.T.C.', name='radioButton6', parent=self.panel1,
              pos=wx.Point(104, 40), size=wx.Size(56, 13), style=0)
        self.radioButton6.SetValue(False)

        self.radioButton3 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON3,
              label='Proveedor', name='radioButton3', parent=self.panel1,
              pos=wx.Point(192, 8), size=wx.Size(81, 13), style=0)
        self.radioButton3.SetValue(False)

        self.radioButton4 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON4,
              label='Tipo Operacion', name='radioButton4', parent=self.panel1,
              pos=wx.Point(192, 24), size=wx.Size(96, 13), style=0)
        self.radioButton4.SetValue(False)

        self.radioButton5 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON5,
              label='Precio', name='radioButton5', parent=self.panel1,
              pos=wx.Point(192, 40), size=wx.Size(56, 13), style=0)
        self.radioButton5.SetValue(False)

        self.staticText1 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT1,
              label='Ordenado por....', name='staticText1', parent=self.panel1,
              pos=wx.Point(8, 8), size=wx.Size(83, 13), style=0)

        self.button1 = wx.Button(id=wxID_INVENTARIOAJUSTEAPROBARBUTTON1,
              label='Buscar', name='button1', parent=self.panel1,
              pos=wx.Point(304, 8), size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnBuscar,
              id=wxID_INVENTARIOAJUSTEAPROBARBUTTON1)

        self.button4 = wx.Button(id=wxID_INVENTARIOAJUSTEAPROBARBUTTON4,
              label='Limpiar', name='button4', parent=self.panel1,
              pos=wx.Point(304, 32), size=wx.Size(75, 23), style=0)
        self.button4.Bind(wx.EVT_BUTTON, self.OnLimpiar,
              id=wxID_INVENTARIOAJUSTEAPROBARBUTTON4)

        self.button3 = wx.Button(id=wxID_INVENTARIOAJUSTEAPROBARBUTTON3,
              label='Procesar', name='button3', parent=self.panel1,
              pos=wx.Point(944, 16), size=wx.Size(75, 23), style=0)
        self.button3.Bind(wx.EVT_BUTTON, self.OnProcesar,
              id=wxID_INVENTARIOAJUSTEAPROBARBUTTON3)

        self.button2 = wx.Button(id=wxID_INVENTARIOAJUSTEAPROBARBUTTON2,
              label='Salir', name='button2', parent=self.panel1,
              pos=wx.Point(1040, 16), size=wx.Size(75, 23), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnSalir,
              id=wxID_INVENTARIOAJUSTEAPROBARBUTTON2)

        self.grid1 = wx.grid.Grid(id=wxID_INVENTARIOAJUSTEAPROBARGRID1,
              name='grid1', parent=self.panel1, pos=wx.Point(8, 80),
              size=wx.Size(1104, 464), style=0)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK,
              self.OnSeleccionarRenglon)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_RIGHT_DCLICK, self.OnHistorico)

        self.staticText2 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT2,
              label='Sucursales', name='staticText2', parent=self.panel1,
              pos=wx.Point(408, 8), size=wx.Size(61, 13), style=0)
        self.staticText2.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              'Tahoma'))

        self.staticText3 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT3,
              label='1. Casa Central', name='staticText3', parent=self.panel1,
              pos=wx.Point(408, 24), size=wx.Size(75, 13), style=0)

        self.staticText4 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT4,
              label='2. Parana', name='staticText4', parent=self.panel1,
              pos=wx.Point(408, 40), size=wx.Size(47, 13), style=0)

        self.staticText5 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT5,
              label='3. Saska', name='staticText5', parent=self.panel1,
              pos=wx.Point(496, 24), size=wx.Size(41, 13), style=0)

        self.staticText6 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT6,
              label='4. Peatonal Sta Fe', name='staticText6',
              parent=self.panel1, pos=wx.Point(496, 40), size=wx.Size(89, 13),
              style=0)

        self.staticText7 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT7,
              label='Pendiente', name='staticText7', parent=self.panel1,
              pos=wx.Point(608, 24), size=wx.Size(48, 13), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL1,
              name='textCtrl1', parent=self.panel1, pos=wx.Point(664, 24),
              size=wx.Size(64, 21), style=0, value='')
        self.textCtrl1.SetEditable(False)

        self.textCtrl2 = wx.TextCtrl(id=wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL2,
              name='textCtrl2', parent=self.panel1, pos=wx.Point(664, 48),
              size=wx.Size(64, 21), style=0, value='')
        self.textCtrl2.SetEditable(False)

        self.staticText8 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT8,
              label='Aprobado', name='staticText8', parent=self.panel1,
              pos=wx.Point(608, 48), size=wx.Size(48, 13), style=0)

        self.textCtrl3 = wx.TextCtrl(id=wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL3,
              name='textCtrl3', parent=self.panel1, pos=wx.Point(728, 24),
              size=wx.Size(64, 21), style=0, value='')
        self.textCtrl3.SetEditable(False)

        self.textCtrl4 = wx.TextCtrl(id=wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL4,
              name='textCtrl4', parent=self.panel1, pos=wx.Point(728, 48),
              size=wx.Size(64, 21), style=0, value='')
        self.textCtrl4.SetEditable(False)

        self.textCtrl5 = wx.TextCtrl(id=wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL5,
              name='textCtrl5', parent=self.panel1, pos=wx.Point(792, 24),
              size=wx.Size(64, 21), style=0, value='')
        self.textCtrl5.SetEditable(False)

        self.textCtrl6 = wx.TextCtrl(id=wxID_INVENTARIOAJUSTEAPROBARTEXTCTRL6,
              name='textCtrl6', parent=self.panel1, pos=wx.Point(792, 48),
              size=wx.Size(64, 21), style=0, value='')
        self.textCtrl6.SetEditable(False)

        self.staticText9 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT9,
              label='Tipo', name='staticText9', parent=self.panel1,
              pos=wx.Point(608, 0), size=wx.Size(51, 18), style=0)

        self.staticText10 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT10,
              label='positivo', name='staticText10', parent=self.panel1,
              pos=wx.Point(664, 0), size=wx.Size(51, 16), style=0)
        self.staticText10.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText11 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT11,
              label='negativo', name='staticText11', parent=self.panel1,
              pos=wx.Point(728, 0), size=wx.Size(57, 16), style=0)
        self.staticText11.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText12 = wx.StaticText(id=wxID_INVENTARIOAJUSTEAPROBARSTATICTEXT12,
              label='Saldo', name='staticText12', parent=self.panel1,
              pos=wx.Point(808, 0), size=wx.Size(35, 16), style=0)
        self.staticText12.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.radioButton7 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON7,
              label='Codigo Sussan', name='radioButton7', parent=self.panel1,
              pos=wx.Point(104, 56), size=wx.Size(88, 13), style=0)
        self.radioButton7.SetValue(False)

        self.radioButton8 = wx.RadioButton(id=wxID_INVENTARIOAJUSTEAPROBARRADIOBUTTON8,
              label='Estacion/Anio', name='radioButton8', parent=self.panel1,
              pos=wx.Point(192, 56), size=wx.Size(88, 13), style=0)
        self.radioButton8.SetValue(False)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.crearGrilla()
        self.textCtrl1.SetLabel("0.00")
        self.textCtrl2.SetLabel("0.00")
        self.textCtrl3.SetLabel("0.00")
        self.textCtrl4.SetLabel("0.00")
        self.textCtrl5.SetLabel("0.00")
        self.textCtrl6.SetLabel("0.00")
        self.radioButton1.SetFocus()

    def OnSalir(self, event):
        self.Close()

    def ajustarAnchoColumnas(self):
        for y in xrange(self.grid1.GetNumberCols()):
            self.grid1.AutoSizeColumn(y,True)

    def crearGrilla(self):
        aux1 = ['ID','Fecha','Suc.','PTC','Codigo\nSussan','Descripcion']
        aux2 = ['Estacion','Talle','Color','Proveedor','Tipo','Observaciones']
        aux3 = ['Cant.','Precio','Diferencia','Total','Aprobacion']
        columnas = aux1+aux2+aux3
        self.grid1.CreateGrid(1,len(columnas))
        for i, col in enumerate(columnas): 
            self.grid1.SetColLabelValue(i, col)

        self.grid1.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        self.grid1.EnableEditing(False)
        self.grid1.Refresh()

        self.ajustarAnchoColumnas()

    def habilitar(self,valor=False):
        self.radioButton1.Enable(valor)
        self.radioButton2.Enable(valor)
        self.radioButton3.Enable(valor)
        self.radioButton4.Enable(valor)
        self.radioButton5.Enable(valor)
        self.radioButton6.Enable(valor)
        self.radioButton7.Enable(valor)
        self.radioButton8.Enable(valor)

    def OnLimpiar(self, event):
        self.grid1.ClearGrid()
        for i in xrange(self.grid1.GetNumberRows()):
            self.grid1.DeleteRows()
        self.grid1.AppendRows()
        self.habilitar(True)
        self.radioButton1.SetFocus()
        self.totalpendientesuma = Decimal("0.00").quantize(Decimal("0.10"))
        self.totalpendienteresta = Decimal("0.00").quantize(Decimal("0.10"))
        self.totalaprobadosuma = Decimal("0.00").quantize(Decimal("0.10"))
        self.totalaprobadoresta = Decimal("0.00").quantize(Decimal("0.10"))
        self.textCtrl1.SetLabel("0.00")
        self.textCtrl2.SetLabel("0.00")
        self.textCtrl3.SetLabel("0.00")
        self.textCtrl4.SetLabel("0.00")
        self.textCtrl5.SetLabel("0.00")
        self.textCtrl6.SetLabel("0.00")

    def OnHistorico(self, event):
        fila = self.grid1.GetGridCursorRow()
        import HistoricoProducto
        appBack = HistoricoProducto.create(None,self.grid1.GetCellValue(fila,3))
        appBack.Show()
        return
    
    def OnSeleccionarRenglon(self, event):
        fila = self.grid1.GetGridCursorRow()
        color = BLANCO
        if self.grid1.GetCellBackgroundColour(fila,0)==BLANCO:
            color=VERDE
            self.grid1.SetCellValue(fila,self.grid1.GetNumberCols()-1,"OK")
        if self.grid1.GetCellBackgroundColour(fila,0)==VERDE:
            color=ROJO
            self.grid1.SetCellValue(fila,self.grid1.GetNumberCols()-1,"BORRAR")
        if self.grid1.GetCellBackgroundColour(fila,0)==ROJO:
            color=BLANCO
            self.grid1.SetCellValue(fila,self.grid1.GetNumberCols()-1,"PENDIENTE")

        for col in xrange(self.grid1.GetNumberCols()):
            self.grid1.SetCellBackgroundColour(fila,col,color)

        self.actualizaTotalizador(fila)
        self.grid1.Refresh()

    def OnBuscar(self, event):
        self.habilitar(False)
        condicion = ""
        #fecha,sucursal,ptc,proveedor,tipo operacion,precio
        if self.radioButton1.GetValue():
            condicion = " ORDER BY fecha,id "
        if self.radioButton2.GetValue():
            condicion = " ORDER BY sucursal,fecha,proveedor,codsussan,ptc "
        if self.radioButton6.GetValue():
            condicion = " ORDER BY ptc,id "
        if self.radioButton3.GetValue():
            condicion = " ORDER BY proveedor,fecha,codsussan,ptc "
        if self.radioButton4.GetValue():
            condicion = " ORDER BY tipooperacion,fecha,codsussan,ptc, "
        if self.radioButton5.GetValue():
            condicion = " ORDER BY precio DESC"
        if self.radioButton7.GetValue():
            condicion = " ORDER BY codsussan,ptc DESC"
        if self.radioButton8.GetValue():
            condicion = " ORDER BY estacion,proveedor,codsussan,ptc "
        sql = "SELECT * FROM ajustes WHERE aprobacion='0' AND aprobacion='0' "+condicion
        resultados = consultasBD.ordenCrudaDict(sql)
        if resultados == () or resultados == None:
            utiles.mensaje(None,"No existen datos")
            return
        self.totalpendientesuma = Decimal("0.00").quantize(Decimal("0.10"))
        self.totalpendienteresta = Decimal("0.00").quantize(Decimal("0.10"))
        self.totalaprobadosuma = Decimal("0.00").quantize(Decimal("0.10"))
        self.totalaprobadoresta = Decimal("0.00").quantize(Decimal("0.10"))
        #id,Fecha,Suc.,PTC,Codigo Sussan,Descripcion,Estacion,Talle,Color
        #0  1     2    3   4             5           6        7     8
        #Proveedor,Tipo Operacion,Observaciones,Cant.,Precio,Total,Aprobacion
        #9         10             11            12    13     14    15
        for fila,item in enumerate(resultados):
            self.grid1.SetCellValue(fila,0,str(item["id"]))
            self.grid1.SetCellValue(fila,1,utiles.invertirFecha2(str(item["fecha"])))
            self.grid1.SetCellValue(fila,2,str(item["sucursal"]))
            self.grid1.SetCellAlignment(fila,2,wx.ALIGN_CENTRE,wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(fila,3,item["ptc"])
            self.grid1.SetCellValue(fila,4,item["codsussan"])
            self.grid1.SetCellValue(fila,5,item["descripcion"][:20])
            self.grid1.SetCellValue(fila,6,item["estacion"])
            self.grid1.SetCellValue(fila,7,item["talle"])
            self.grid1.SetCellValue(fila,8,item["color"])
            self.grid1.SetCellValue(fila,9,item["proveedor"][:15])
            if item["tipooperacion"] == 0:
                self.grid1.SetCellValue(fila,10,"+")
            else:
                self.grid1.SetCellValue(fila,10,"-")
            self.grid1.SetCellValue(fila,11,item["observaciones"][:20])
            self.grid1.SetCellValue(fila,12,str(item["cantidad"]))
            self.grid1.SetCellAlignment(fila,12,wx.ALIGN_RIGHT,wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(fila,13,str(item["precio"]))
            self.grid1.SetCellAlignment(fila,13,wx.ALIGN_RIGHT,wx.ALIGN_CENTRE)
            precio = Decimal(self.grid1.GetCellValue(fila,13))
            self.grid1.SetCellValue(fila,14,str(item["diferencia"]))
            self.grid1.SetCellAlignment(fila,14,wx.ALIGN_RIGHT,wx.ALIGN_CENTRE)
            cantidad = Decimal(self.grid1.GetCellValue(fila,14))
            monto = (precio*cantidad).quantize(Decimal("0.10"))
            self.grid1.SetCellValue(fila,15,str(monto))
            self.grid1.SetCellAlignment(fila,15,wx.ALIGN_RIGHT,wx.ALIGN_CENTRE)
            self.grid1.SetCellValue(fila,16,"PENDIENTE")
            
            self.grid1.AppendRows()

        for fila in xrange(self.grid1.GetNumberRows()):
            if self.grid1.GetCellValue(fila,0)=="":
                continue
            self.actualizaTotalizador(fila,True)

        self.ajustarAnchoColumnas()

    def actualizaTotalizador(self,fila,inicial=False):
        #id,Fecha,Suc.,PTC,Codigo Sussan,Descripcion,Estacion,Talle,Color,Proveedor
        #0  1     2    3   4             5           6        7     8     9
        #Tipo Operacion,Observaciones,Cant.,Precio,Diferencia,Total,Aprobacion
        #10             11            12    13     14         15    16
        
        cantidad = Decimal(self.grid1.GetCellValue(fila,14))
        precio = Decimal(self.grid1.GetCellValue(fila,13))
        monto = (precio*cantidad).quantize(Decimal("0.10"))
        signo = self.grid1.GetCellValue(fila,10)
        aprobado = self.grid1.GetCellValue(fila,self.grid1.GetNumberCols()-1)
        
        if signo=='+' and aprobado=='PENDIENTE':
            self.totalpendientesuma+=monto
        if signo=='-' and aprobado=='PENDIENTE':
            self.totalpendienteresta+=monto
        if signo=='+' and aprobado=='OK':
            self.totalaprobadosuma+=monto
            if not inicial:
                self.totalpendientesuma-=monto
        if signo=='-' and aprobado=='OK':
            self.totalaprobadoresta+=monto
            if not inicial:
                self.totalpendienteresta-=monto

        if signo=='+' and aprobado=='BORRAR' and not inicial:
            self.totalaprobadosuma-=monto
        if signo=='-' and aprobado=='BORRAR' and not inicial:
            self.totalaprobadoresta-=monto

        self.textCtrl1.SetLabel(str(self.totalpendientesuma))
        self.textCtrl3.SetLabel(str(self.totalpendienteresta))
        self.textCtrl5.SetLabel(str(self.totalpendientesuma-self.totalpendienteresta))
        
        self.textCtrl2.SetLabel(str(self.totalaprobadosuma))
        self.textCtrl4.SetLabel(str(self.totalaprobadoresta))
        self.textCtrl6.SetLabel(str(self.totalaprobadosuma-self.totalaprobadoresta))

    def OnProcesar(self, event):
        print "Actualizando inventario"
        for fila in xrange(self.grid1.GetNumberRows()):
            if self.grid1.GetCellValue(fila,0)=="":
                continue
            if self.grid1.GetCellValue(fila,self.grid1.GetNumberCols()-1)=="PENDIENTE":
                continue
        #id,Fecha,Suc.,PTC,Codigo Sussan,Descripcion,Estacion,Talle,Color,Proveedor
        #0  1     2    3   4             5           6        7     8     9
        #Tipo Operacion,Observaciones,Cant.,Precio,Diferencia,Total,Aprobacion
        #10             11            12    13     14         15    16

            if self.grid1.GetCellValue(fila,self.grid1.GetNumberCols()-1)=="OK":
                sucursal = self.grid1.GetCellValue(fila,2)
                ptc = self.grid1.GetCellValue(fila,3)
                cantidad = str(Decimal(self.grid1.GetCellValue(fila,12)))

                tabla = "stock"
                campos="stock='"+str(cantidad)+"'"
                condicion =  "ptc='"+ptc+"' AND sucursal='"+sucursal+"'"

                #el mismo registro debe grabarse en cada archivo stock de cada sucursal
                conecta = True
                for i in xrange(4):
                    try:
                        print "Grabando en",i+1,"Suc:",sucursal,"PTC:",ptc,"Cant:",cantidad,
                        consultasBD.executaUpdateSQLRemoto(None,tabla,campos,condicion,i)
                    except:
                        conecta = False
                        print "ERROR",
                        break
                print
                if conecta:
                    #todo se grabo bien en forma remota, actualizo BD local
                    tabla = "ajustes"
                    fecha = utiles.obtenerFecha("Y-m-d")
                    campos="aprobacion='1',fechaaprobado='"+fecha+"'"
                    id = self.grid1.GetCellValue(fila,0)
                    condicion =  "id='"+id+"' \n"
                    consultasBD.executaUpdateSQL(None,tabla,campos,condicion)
            else:
                #se marca borrado en la BD local
                tabla = "ajustes"
                fecha = utiles.obtenerFecha("Y-m-d")
                campos="aprobacion='2',fechaaprobado='"+fecha+"'"
                id = self.grid1.GetCellValue(fila,0)
                condicion =  "id='"+id+"' \n"
                consultasBD.executaUpdateSQL(None,tabla,campos,condicion)
        
        utiles.mensaje(None,"Actualizaciones procesadas")
        self.OnLimpiar(None)
        self.OnBuscar(None)
